/*
* @Author: yanxiaoli
* @Date:   2018-10-18 17:27:11
* @Last Modified by:   Administrator
* @Last Modified time: 2018-10-19 11:36:06
*/
$.fn.linkage = function(province, city, selectBox) {
    var provinceInput = province + ' input';
    var cityInput = city + ' input';
    var provinceUl = province + ' ul';
    var cityUl = city + ' ul';
    var pList = provinceList;
    var dList = provinceList[0].cityList[0].nameList;
    // -------------------------------------------------------
    $(selectBox).click(function(e) {
        $(this).siblings().find('ul').hide();
        $(this).find('ul').toggle();
        e.stopPropagation();
    });
    /*-------------------------------默认城市开始--------------------------------------*/
    
    for (var p = 0; p < pList.length; p++) { //省循环列表
        var provinceName = pList[p].name;
        var pLi = "<li>" + provinceName + "</li>";
        $(province).find('ul').append(pLi);
    }
   
    for (var a = 0; a < dList.length; a++) { //城市循环列表
       var cityName = dList[a];
        var dLi = "<li>" + cityName + "</li>";
        $(city).find('ul').append(dLi);
    }
    /*-------------------------------默认城市结束--------------------------------------*/
   
    $(provinceUl).on('click', 'li', function(e) { //省级
            var pLiText = $(this).text();
            $(province).find('input').val(pLiText);
            var pLiIndex = $(this).index();
            var indexcity = provinceList[pLiIndex].cityList[0].nameList;
           
           $(city).find('ul').html('');
            for (var j = 0; j < indexcity.length; j++) {
                var cityname = indexcity[j];
                var cityli = "<li>" + cityname + "</li>";
                $(city).find('ul').append(cityli);
            }
        
            $(cityInput).val(indexcity[0]);
            $('#pLiIndex').val(pLiIndex);
            $(this).parent('ul').hide();
            e.stopPropagation();
 
        })
      
    $(cityUl).on('click', 'li', function(e) { //城市
            var cityText = $(this).text();
            $(city).find('input').val(cityText);
            $(this).parent('ul').hide();
            e.stopPropagation();
        })
      
}
 
 
var provinceList = [{
    name: '北京',
    cityList: [{
        nameList: ['北京']
    }]
}, {
    name: '上海',
    cityList: [{
        nameList: ['上海']
    }]
}, {
    name: '广东',
    cityList: [{
        nameList: ['广州', '深圳', '珠海', '佛山', '惠州', '清远', '东莞', '中山']
    }]
}, {
    name: '天津',
    cityList: [{
        nameList: ['天津']
    }]
}, {
    name: '四川',
    cityList: [{
        nameList: ['成都', '德阳', '乐山', '眉山']
    }]
}, {
    name: '江苏',
    cityList: [{
        nameList: ['南京', '无锡', '徐州', '苏州', '南通', '镇江']
    }]
}, {
    name: '浙江',
    cityList: [{
        nameList: ['杭州', '嘉兴', '绍兴']
    }]
}, {
    name: '山东',
    cityList: [{
        nameList: ['济南', '青岛', '烟台', '潍坊', '威海']
    }]
}, {
    name: '辽宁',
    cityList: [{
        nameList: ['沈阳', '大连']
    }]
}, {
    name: '福建',
    cityList: [{
        nameList: ['厦门', '泉州', '漳州', '龙岩']
    }]
}, {
    name: '湖北',
    cityList: [{
        nameList: ['武汉', '黄冈', '咸宁']
    }]
}, {
    name: '重庆',
    cityList: [{
        nameList: ['重庆']
    }]
}, {
    name: '湖南',
    cityList: [{
        nameList: ['长沙']
    }]
}, {
    name: '陕西',
    cityList: [{
        nameList: ['西安']
    }]
}, {
    name: '河北',
    cityList: [{
        nameList: ['石家庄', '秦皇岛', '邯郸', '邢台', '保定', '张家口', '承德', '廊坊','衡水']
    }]
}, {
    name: '山西',
    cityList: [{
        nameList: ['太原', '晋中']
    }]
}, {
    name: '海南',
    cityList: [{
        nameList: ['海口', '三亚', '五指山', '琼海', '儋州', '文昌','万宁','定安','澄迈','临高','乐东','陵水','保亭','琼中']
    }]
}, {
    name: '安徽',
    cityList: [{
        nameList: ['合肥', '滁州']
    }]
}, {
    name: '云南',
    cityList: [{
        nameList: ['昆明', '西双版纳', '大理']
    }]
}, {
    name: '河南',
    cityList: [{
        nameList: ['郑州']
    }]
}, {
    name: '吉林',
    cityList: [{
        nameList: ['长春']
    }]
}, {
    name: '贵州',
    cityList: [{
        nameList: ['贵阳']
    }]
}, {
    name: '内蒙古',
    cityList: [{
        nameList: ['呼和浩特']
    }]
}];
